package com.fourhats.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.fourhats.dao.CategoryDAO;
import com.fourhats.model.Category;

@Repository
public class CategoryDAOImpl extends GenericDAOImpl<Category, Long> implements CategoryDAO {

	protected CategoryDAOImpl() {
		super(Category.class.getName());
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> getCategories() {
		DetachedCriteria criteria = DetachedCriteria.forEntityName(entityName);
        criteria.add(Restrictions.eq("enable", true));
        criteria.addOrder(Order.asc("name"));
        return getHibernateTemplate().findByCriteria(criteria);
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> getCategoriesByKeyword(String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forEntityName(entityName);
        criteria.add(Restrictions.ilike("name", keyword, MatchMode.ANYWHERE));
        criteria.add(Restrictions.eq("enable", true));
        return getHibernateTemplate().findByCriteria(criteria);
	}
}